﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DocumentMakingTools.Services
{
    /// <summary>
    /// Class that process the keyword replace
    /// </summary>
    static class KeywordReplaceService
    {
        /// <summary>
        /// Replace all oldkey with the newkey in a directory
        /// </summary>
        /// <param name="DirectoryName">Directory Name</param>
        /// <param name="OldKey">The old keyword to be replaced</param>
        /// <param name="NewKey">The new keyword</param>
        /// <exception cref="NotImplementedException"></exception>
        public static void ReplaceKeyword(string DirectoryName, string OldKey, string NewKey)
        {
            // if folder has neither sub folder nor files, rename folder directly if needed;
            // else if folder has sub folder , replace keyword recursive;
            // else if folder has files , rename filename if needed and replace keyword in its content if needed.

            if (string.IsNullOrEmpty(DirectoryName) || string.IsNullOrEmpty(OldKey) || NewKey == null)
                throw new ArgumentNullException($"{nameof(Directory)} or {nameof(OldKey)} couldn't be empty");

            if (Directory.Exists(DirectoryName))
                ReplaceKeywordRecursive(new DirectoryInfo(DirectoryName), OldKey, NewKey);
            else
                throw new IOException("Directory Not Exists");
        }

        /// <summary>
        /// Recursive replace all oldkey with the newkey in a directory
        /// </summary>
        /// <param name="Directory">Directory</param>
        /// <param name="OldKey">The old keyword to be replaced</param>
        /// <param name="NewKey">The new keyword</param>
        private static void ReplaceKeywordRecursive(DirectoryInfo Directory, string OldKey, string NewKey)
        {
            var subFiles = Directory.GetFiles();
            if (subFiles.Any())
                Task.Factory.StartNew(() => subFiles.AsParallel().ForAll((subFileItem) => CheckAndRenameFile(subFileItem, OldKey, NewKey)));

            var subDirectories = Directory.GetDirectories();
            if (subDirectories.Any() /* && match the old key*/)
                RenameDirectory(Directory, NewKey);
            else
                Task.Factory.StartNew(() => subDirectories.AsParallel().ForAll((subDirectoryItem) => ReplaceKeywordRecursive(subDirectoryItem, OldKey, NewKey)));
        }

        /// <summary>
        /// Rename the directory
        /// </summary>
        /// <param name="Directory">Directory need to be renamed</param>
        /// <param name="NewName">New name</param>
        /// <exception cref="NotImplementedException"></exception>
        private static void RenameDirectory(DirectoryInfo Directory, string NewName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Check whether the file should be renamed , if needed , rename filename and replace content
        /// </summary>
        /// <param name="File">File need to be Checked</param>
        /// <param name="OldKey">Old Keyword</param>
        /// <param name="NewKey">New keyword</param>
        /// <exception cref="NotImplementedException"></exception>
        private static void CheckAndRenameFile(FileInfo File, string OldKey, string NewKey)
        {
            // Warning : This function is core , and has a bit hard to implement.
            throw new NotImplementedException();
        }
    }
}

